<?php
// +----------------------------------------------------------------------
// | XPFRAMEWORK  [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2024 https://gitee.com/sdnode All rights reserved.
// +----------------------------------------------------------------------
// | Author: chenshiming
// +----------------------------------------------------------------------

namespace app\admin\controller\csmwj;

use addons\csmwj\library\xapp\csmwj\base\XpADashboard;

class XpPaperDashboard extends XpADashboard
{
    protected $noNeedLogin = ["*"];
    public function index()
    {
        //总多少会议
        if (true) {
            $dao = new \app\admin\model\csmwj\Paper();
            $cnt = $dao->where('status', '=', 'normal')->count();
            $this->assign('total1', $cnt);
        }
        //总多少人数
        if (true) {
            $dao =  new \app\admin\model\csmwj\Userpaper();
            $cnt = $dao->where('iseffectverison', '=', 'Y')
                ->where('answerstatus', 'in', ['FINISH'])
                ->count();
            $this->assign('total2', $cnt);
        }

        $dao = new \app\admin\model\csmwj\Paper();
        $sql = $dao->alias("t1")
            ->join("csmwj_userpaper t2", "t1.id=t2.csmwj_paper_id and t2.iseffectverison='Y'")
            ->where("t1.status", "=", "normal")
            ->limit(0, 10);
        //今日汇总
        if (true) {
            //签到人数
            $box1a = (clone $sql)
                ->where('t2.statdate', '>=', static::get_datestring_beginclock(0))
                ->where('t2.statdate', '<=', static::get_datestring_endclock(0))
                ->where('t2.answerstatus', 'in', ['FINISH'])
                ->count();
            $this->assign('box1a', $box1a);
            //报名人数
            $box1b = (clone $sql)
                ->where('t2.createtime', '>=', static::get_datetime_beginclock(0))
                ->where('t2.createtime', '<=', static::get_datetime_endclock(0))
                ->count();
            $this->assign('box1b', $box1b);
        }

        //今日列表
        if (true) {
            $box1list = (clone $sql)
                ->field("t1.name,count(*) val")
                ->where('t2.statdate', '>=', static::get_datestring_beginclock(0))
                ->where('t2.statdate', '<=', static::get_datestring_endclock(0))
                ->where('t2.answerstatus', 'in', ['FINISH'])
                ->group("t1.name")
                ->order('val', 'desc')
                ->select();
            $this->assign('box1list', $box1list);
        }


        //7天汇总
        if (true) {
            $box2a = (clone $sql)
                ->where('t2.statdate', '>=', static::get_datestring_beginclock(-7))
                ->where('t2.statdate', '<=', static::get_datestring_endclock(0))
                ->where('t2.answerstatus', 'in', ['FINISH'])
                ->count();
            $this->assign('box2a', $box2a);

            $box2b = (clone $sql)
                ->where('t2.createtime', '>=', static::get_datetime_beginclock(-7))
                ->where('t2.createtime', '<=', static::get_datetime_endclock(0))
                ->count();
            $this->assign('box2b', $box2b);
        }
        //7天列表
        if (true) {
            $box2list = (clone $sql)
                ->field("t1.name,count(*) val")
                ->where('t2.statdate', '>=', static::get_datestring_beginclock(-7))
                ->where('t2.statdate', '<=', static::get_datestring_endclock(0))
                ->where('t2.answerstatus', 'in', ['FINISH'])
                ->group("t1.name")
                ->order('val', 'desc')
                ->select();
            $this->assign('box2list', $box2list);
        }

        //30天汇总
        if (true) {
            $box3a = (clone $sql)
                ->where('t2.statdate', '>=', static::get_datestring_beginclock(-30))
                ->where('t2.statdate', '<=', static::get_datestring_endclock(0))
                ->where('t2.answerstatus', 'in', ['FINISH'])
                ->count();
            $this->assign('box3a', $box3a);

            $box3b = (clone $sql)
                ->where('t2.createtime', '>=', static::get_datetime_beginclock(-30))
                ->where('t2.createtime', '<=', static::get_datetime_endclock(0))
                ->count();
            $this->assign('box3b', $box3b);
        }

        //30天列表
        if (true) {
            $box3list = (clone $sql)
                ->field("t1.name,count(*) val")
                ->where('t2.statdate', '>=', static::get_datestring_beginclock(-30))
                ->where('t2.statdate', '<=', static::get_datestring_endclock(0))
                ->where('t2.answerstatus', 'in', ['FINISH'])
                ->group("t1.name")
                ->order('val', 'desc')
                ->select();
            $this->assign('box3list', $box3list);
        }


        //总汇总
        if (true) {
            // 进行中
            $map1['cfghasdatelimit'] = ['=', 'Y'];
            $map1['cfgbegintime'] = ['<=', time()];
            $map1['cfgendtime'] = ['>=', time()];
            $map2['cfghasdatelimit'] = ['=', 'N'];
            $dao = new \app\admin\model\csmwj\Paper();
            $box4a = $dao->alias('t1')
                ->where('t1.status', '=', 'normal')
                ->where(function ($query) use ($map1, $map2) {
                    $query->where(function ($query) use ($map1, $map2) {
                        $query->where($map1);
                    })->whereOr($map2);
                })
                ->count('distinct t1.id');
            $this->assign('box4a', $box4a);

            //即将进行
            $box4b = $dao->alias('t1')
                ->where('t1.status', '=', 'normal')
                ->where('t1.cfghasdatelimit', '=', 'Y')
                ->where('t1.cfgbegintime', '>=', time())
                ->count('distinct t1.id');
            $this->assign('box4b', $box4b);
        }


        //总列表
        if (true) {
            $dao = new \app\admin\model\csmwj\Paper();
            $box4list = $dao->alias('t1')
                ->field("t1.name,t1.cfgendtime val,t1.cfghasdatelimit")
                ->where('t1.status', '=', 'normal')
                ->order('t1.id', 'desc')
                ->limit(0, 10)
                ->select();
            $this->assign('box4list', $box4list);
        }
        return $this->view->fetch();
    }


    public function indexChartDataset()
    {
        $list = null;
        if (true) {
            $dao = new \app\admin\model\csmwj\Paper();
            $getDateArray = $this->getDateArray(-7, 0);
            //$applydate = static::get_mysqlfield_datefromtimstamp('t2.statdate');
            $list = $dao->alias("t1")
                ->field("'' name,t2.statdate applydate,count(*) val1,count(distinct csmwj_weixinuser_id) val2")
                ->join("csmwj_userpaper t2", "t2.csmwj_paper_id=t1.id and t2.iseffectverison='Y'", "left")
                ->where("t1.status", "=", "normal")
                ->where("t2.answerstatus", 'in', ['FINISH'])
                ->where('t2.statdate', '>=', static::get_datestring_beginclock(-7))
                ->where('t2.statdate', '<=', static::get_datestring_endclock(0))
                ->group("statdate")
                ->select();
        }
        $this->success('', null, [
            'option' => $this->getObjectDateOption(-3, +3, $list, 'name', 'applydate', ['val1', 'val2']),
        ]);
    }


    public static function get_datestring_beginclock($sepdate)
    {
        return date('Y-m-d', strtotime($sepdate . " day")); //2022-07-24
    }
    public static function get_datestring_endclock($sepdate)
    {
        return date('Y-m-d', strtotime($sepdate . " day")); //2022-07-24
    }
    public static function get_datetime_beginclock($sepdate)
    {
        return strtotime(static::get_datetimestring_beginclock($sepdate));
    }
    public static function get_datetime_endclock($sepdate)
    {
        return strtotime(static::get_datetimestring_endclock($sepdate));
    }
    public static function get_datetimestring_beginclock($sepdate)
    {
        return date('Y-m-d 00:00:00', strtotime($sepdate . " day")); //2022-07-24 00:00:00
    }
    public static function get_datetimestring_endclock($sepdate)
    {
        return date('Y-m-d 23:59:59', strtotime($sepdate . " day")); //2022-07-24 23:59:59
    }
}
